<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-02-24 15:38:42
 * @Description: 供货商的model
 */
class MaintenanceSupplier extends Eloquent
{
    protected $table = "maintenance_supplier";

    public static $created_types =  [1=>'ERP',2=>'PMS',3=>'ARC',4=>'SCS'];
    public static $purchase_types = ['1'=>'集采','2'=>'临采'];
    public static $purchase_statuss = ['1'=>'开启','0'=>'关闭'];
    public static $regular_pay = ['1'=>'定期结算','0'=>'现金结算'];

    /**
     * @var array  合同状态数组
     */
    public static $contract_status_arr = [
        '1' => '未签合同',
        '2' => '有效',
        '3' => '无效',
        '4' => '已延期',
    ];
    
    /**
     * @var array  合作状态数组
     */
    public static $work_together_type_arr = [
        '1' => '正常合作',
        '2' => '暂停合作',
        '3' => '终止合作',
    ];
    
    /**
     * @var array  结款方式数组
     */
    public static $regular_pay_arr = [
        '0' => '现金',
        '1' => '月结',
        '2' => '按区域设置',
        '3' => '按门店设置',
    ];
    
    /**
     * @var array  物流配送数组
     */
    public static $businss_express_arr = [
        '1' => '安惠配送',
        '2' => '供应商配送',
        '3' => '第三方物流',
    ];
    
    /**
     * @var array  含税数组
     */
    public static $tax_arr = [
        '1' => '含税',
        '2' => '不含税',
    ];
    
    /**
     * @var array  物流运费数组
     */
    public static $logistics_freight_arr = [
        '1' => '含运费',
        '2' => '不含运费',
        '3' => '按区域设置',
        '4' => '按门店设置',
    ];

    /**
     * @param $supplierId   integer    供货商Id
     * @param $shopId      integer     当前门店Id
     * @param $city_id      integer     适配城市Id
     * @param $response    array        组织返回
     * @return array
     * @author yyc
     * @date 2018/9/14 16:16
     */
    public static function getPayTaxFreight($supplierId, $shopId, &$response, $city_id){
        $supplier = MaintenanceSupplier::where('id', $supplierId)
            ->select(['logistics_freight', 'tax', 'regular_pay', 'id', 'name'])->first();

        if (is_null($supplier)) {           //判断供应商是否存在
            $response = Lang::get("scs_error.supplier_error_10013");
            return null;               //没有返回
        }
        $supplier->status = 1;

        if ($supplier->logistics_freight == 0 || $supplier->tax == 0) {
            $supplier->status = 2;
        }

        //物流运费信息
        if ($supplier->logistics_freight == 3) {
            //安区域
            $freight = MaintenanceSupplierLogisticsFreightArea::where('supplier_id', $supplierId)->where('city_id', $city_id)->first();
            if (is_null($freight)) {
                $supplier->status = 2;
                $supplier->logistics_freight = 555;
                $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
            } else {
                $supplier->logistics_freight = $freight->logistics_freight;
            }
        } elseif ($supplier->logistics_freight == 4) {
            //安门店
            $freight = MaintenanceSupplierLogisticsFreightShop::where('shop_id', $shopId)->where('supplier_id', $supplierId)->first();
            if (is_null($freight)) {
                $supplier->status = 2;
                $supplier->logistics_freight = 555;
                $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
            } else {
                $supplier->logistics_freight = $freight->logistics_freight;
            }
        }

        //结款方式
        if ($supplier->regular_pay == 2) {
            //安区域
            $regular_pay = MaintenanceSupplierPayArea::where('supplier_id', $supplierId)->where('city_id', $city_id)->first();
            if (is_null($regular_pay)) {
                $supplier->status = 2;
                $supplier->regular_pay = 555;
                $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
            } else {
                $supplier->regular_pay = $regular_pay->regular_pay;
            }

        } elseif ($supplier->regular_pay == 3) {
            //安门店
            $regular_pay = MaintenanceSupplierPayShop::where('shop_id', $shopId)->where('supplier_id', $supplierId)->first();
            if (is_null($regular_pay)) {
                $supplier->status = 2;
                $supplier->regular_pay = 555;
                $response = Lang::get('scs_error.maintenance_supplier_pay_tax_freight_error_130001');
            } else {
                $supplier->regular_pay = $regular_pay->regular_pay;
            }
        }

        return $supplier;
    }

}